import axios from 'axios'
import router from '../router'
import baseUrl from '../setBaseUrl.ts'
// const baseURL = 'http://172.16.2.68:8088/api/v1'
// axios.defaults.baseURL = '/api/v1/'
// axios.defaults.baseURL = process.env.API_HOST + '/api/v1/'
// axios.defaults.baseURL = baseURL
axios.defaults.withCredentials = false
axios.defaults.baseURL = baseUrl
// console.log(baseUrl)
// const host = process.env.API_HOST
// console.log(host)
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 将token给到一个前后台约定好的key中，作为请求发送
  let token = localStorage.getItem('mytoken')
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// http response 服务器响应拦截器，这里拦截401错误，并重新跳入登页重新获取token
axios.interceptors.response.use(response => {
  return response
}, error => {
  // console.log(error.response)
  if (error.response) {
    switch (error.response.status) {
      case 401:
      // 返回 401 清除token信息并跳转到登录页面
        // 清除登录状态;
        localStorage.removeItem('mytoken')
        localStorage.removeItem('username')
        router.replace({
          path: 'login'
          // query: { redirect: router.currentRoute.fullPath }
        })
    }
  }
  return Promise.reject(error.response.data)
})
// 登录验证
export const checkUser = params => {
  return axios.get('security/token', params).then(res => res.data)
}
// 获取用户列表
export const getUserList = params => {
  return axios.get('security/sys-users', params).then(res => res.data)
}
// 新增用户 保存
export const addUser = params => {
  return axios.post('security/sys-users', params).then(res => res.data)
}
// 验证用户名重复
export const checkUserName = params => {
  return axios.get(`security/sys-users/exist-user`, params).then(res => res.data)
}
// 根据id获取用户信息
export const getUserById = params => {
  return axios.get(`security/sys-users/${params}`).then(res => res.data)
}
// 保存编辑用户信息
export const editUser = params => {
  return axios.put(`security/sys-users`, params).then(res => res.data)
}
// 删除用户信息
export const deleteUser = params => {
  return axios.delete(`security/sys-users/${params}`).then(res => res.data)
}
// 得到组织机构列表
export const getOrgList = params => {
  return axios.get(`security/sys-orgs/${params}/tree-and-users`).then(res => res.data)
}
// 保存添加组织机构子节点
export const addOrgChild = params => {
  return axios.post('security/sys-orgs/child-node', params).then(res => res.data)
}
// 删除组织机构
export const deleteOrg = params => {
  return axios.delete(`security/sys-orgs/node/${params}`).then(res => res.data)
}
// 保存编辑组织机构信息
export const editOrg = params => {
  return axios.put(`security/sys-orgs/node`, params).then(res => res.data)
}
// 获取未绑定组织机构的用户
export const getfreeUsers = params => {
  return axios.get(`security/sys-orgs/free-users`).then(res => res.data)
}
// 保存组织机构下的用户
export const addOrgUsers = (orgId, userids) => {
  return axios.post(`security/sys-orgs/node/${orgId}/users`, userids).then(res => res.data)
}
// 删除组织机构下的用户
export const deleteOrgUser = (orgId, userid) => {
  return axios.delete(`security/sys-orgs/node/${orgId}/users`, userid).then(res => res.data)
}
// 获取菜单树
export const getMenuTree = params => {
  return axios.get(`security/sys-menus/${params}/tree`).then(res => res.data)
}
// 获取新建编辑菜单时候关键字下拉
export const getMenuKeywords = params => {
  return axios.get(`security/sys-menus/models`).then(res => res.data)
}
// 保存子菜单
export const addMenus = params => {
  return axios.post(`security/sys-menus`, params).then(res => res.data)
}
// 编辑菜单
export const editMenus = params => {
  return axios.put(`security/sys-menus`, params).then(res => res.data)
}
// 删除菜单
export const deleteMenu = params => {
  return axios.delete(`security/sys-menus/${params}`).then(res => res.data)
}
// 验证菜单名称是否存在
export const checkMenuName = params => {
  return axios.get(`security/sys-menus/exist-menu`, params).then(res => res.data)
}
// 角色中获取菜单列表
export const getMenuList = params => {
  return axios.get(`security/sys-roles/${params}/menu-fun-tree`).then(res => res.data)
}
// 获取角色列表
export const getRoleList = params => {
  return axios.get('security/sys-roles', params).then(res => res.data)
}
// 判断角色名称是否已经存在
export const checkRoleName = params => {
  return axios.get(`security/sys-roles/exist-role`, params).then(res => res.data)
}
// 角色中获取组织机构列表
export const getOrgTree = params => {
  return axios.get(`security/sys-roles/${params}/org-user-tree`).then(res => res.data)
}
// 保存新建角色信息
export const addRole = params => {
  return axios.post(`security/sys-roles`, params).then(res => res.data)
}
// 保存修改角色信息
export const editRole = params => {
  return axios.put(`security/sys-roles`, params).then(res => res.data)
}
// 删除角色信息
export const deleteRole = params => {
  return axios.delete(`security/sys-roles/${params}`).then(res => res.data)
}
